package com.xuelang.pipeline.worker.app.configmap.impl;

import com.xuelang.pipeline.worker.app.common.util.K8sUtils;
import com.xuelang.pipeline.worker.app.common.util.VerifyUtil;
import com.xuelang.pipeline.worker.app.configmap.ConfigMapService;
import com.xuelang.pipeline.worker.client.configmap.dto.ConfigMapDTO;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.openapi.models.V1ConfigMapList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author liuyuan.ly
 * @since 2024/7/23 10:43
 */
@Service
@Slf4j
public class ConfigMapServiceImpl implements ConfigMapService {


    @Override
    public V1ConfigMap createVolume(ConfigMapDTO req) {
        VerifyUtil.ObjectNotNull(req.getV1ConfigMap(), "configMap不能为空");
        K8sUtils.initApiClient(req.getKubeConfig());
        CoreV1Api api = new CoreV1Api();
        V1ConfigMap configMap;
        try {
            configMap = api.createNamespacedConfigMap(req.getNamespaceName(), req.getV1ConfigMap(),
                    null, null, null, null);
        } catch (ApiException e) {
            log.info("创建configMap信息失败:{},堆栈信息:", e.getResponseBody(), e);
            throw new RuntimeException(e.getResponseBody());
        }
        return configMap;
    }

    @Override
    public V1ConfigMap saveOrUpdateConfigMap(ConfigMapDTO req) {
        VerifyUtil.verifyStringNotBlank(req.getConfigMapName(), "configMap名字不能为空");
        V1ConfigMapList configMapList = getConfigMapList(req);
        CoreV1Api api = new CoreV1Api();
        V1ConfigMap v1ConfigMap;
        if (!configMapList.getItems().isEmpty()) {
            //存在更新
            V1ConfigMap replaceConfigMap = configMapList.getItems().get(0);
            replaceConfigMap.setData(req.getData());
            try {
                v1ConfigMap = api.replaceNamespacedConfigMap(req.getConfigMapName(), req.getNamespaceName(), replaceConfigMap, null,
                        null, null, null);
            } catch (ApiException e) {
                log.info("更新configMap信息失败:{},堆栈信息:", e.getResponseBody(), e);
                throw new RuntimeException(e.getResponseBody());
            }
        } else {
            //不存在创建
            v1ConfigMap = createVolume(req);
        }

        return v1ConfigMap;
    }

    @Override
    public V1ConfigMapList getConfigMapList(ConfigMapDTO req) {
        K8sUtils.initApiClient(req.getKubeConfig());
        CoreV1Api api = new CoreV1Api();
        String filedName = "";
        if (!StringUtils.isBlank(req.getConfigMapName())) {
            filedName = "metadata.name=" + req.getConfigMapName();
        }
        V1ConfigMapList configMapList;
        try {
            configMapList = api.listNamespacedConfigMap(req.getNamespaceName(), null, Boolean.valueOf("true"),
                    null, filedName, null, null, null,
                    null, null, null);
        } catch (ApiException e) {
            log.info("获取configMap列表信息失败:{},堆栈信息:", e.getResponseBody(), e);
            throw new RuntimeException(e.getResponseBody());
        }
        return configMapList;
    }
}
